# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

pkgname=git
pkgver=2.24.0
pkgrel=1
pkgdesc="The fast distributed version control system"
arch=('i686' 'x86_64')
url="https://git-scm.com/"
license=('GPL2')
groups=('VCS')
depends=('curl'
         'expat>=2.0'
         #'info'
         'libpcre2_8'
         'vim'
         'openssh'
         'openssl'
         'perl-Error'
         'perl>=5.14.0'
         'perl-Authen-SASL'
         'perl-libwww'
         'perl-MIME-tools'
         'perl-Net-SMTP-SSL'
         'perl-TermReadKey')
makedepends=('asciidoc'
             'docbook-xml'
             'docbook-xsl'
             #'docbook2x'
             'less'
             'libcurl-devel'
             'libiconv-devel'
             'pcre2-devel'
             #'python2'
             'rsync'
             #'texinfo'
             'xmlto')
optdepends=(#'tk: gitk and git gui'
            'python2: various helper scripts'
            'subversion: git svn'
            #'cvsps: git cvsimport'
            )
#install=git.install
replaces=('git-core')
provides=('git-core')
#options=('debug' '!strip')
source=("${pkgname}-${pkgver}.tar.gz"::https://github.com/git/git/archive/v${pkgver}.tar.gz
        1.7.9-cygwin.patch
        git-1.9.0-manifest-msys2.patch
        git-1.8.4-msys2.patch
        git-2.3.5-mingw-pwd.patch
        git-2.8.2-Cygwin-Allow-DOS-paths.patch
        git-tcsh-completion-fixes.patch
        git-zsh-completion-fixes.patch)
sha256sums=('6b96ba03bc9e3ae5b8fb32b4bd8546298bdd792995a5d4c87f1f92c3c08aedb5'
            'c5e735d829e11f79e2d508b663d0924030498f48fc716881031fb975dbf187a5'
            '2c0301076a7b60b5be2491433560f6f87c510ec64d2d8f9db5a75c1295893885'
            '340d289f8a9d82975b34bd635e8c734c0c8529d5ac1ad9bbc8a77ed752502b02'
            'f67412f829e026b677d4c3ceacfb21da0549ad4636fb07c283e52c3fd5aa3e22'
            '905e35e145be34f6affad830155a193d869da4898e46726f36cef2e89c073d23'
            'b28b3e13de08911f027d19a557967fc21128318a8d6ca74b20b26ee997a9b992'
            '261438a4bedd5563c702533c34792581ccfa46db873ca3e105e6c1ab803a3107')

prepare() {
  cd "${srcdir}/${pkgname}-${pkgver}"

  rm -f compat/win32/git.manifest compat/win32/resource.rc
  patch -p2 -i "${srcdir}"/1.7.9-cygwin.patch
  patch -p1 -i "${srcdir}"/git-1.8.4-msys2.patch
  patch -p1 -i "${srcdir}"/git-1.9.0-manifest-msys2.patch
  patch -p2 -i "${srcdir}"/git-2.3.5-mingw-pwd.patch
  patch -p1 -i "${srcdir}"/git-2.8.2-Cygwin-Allow-DOS-paths.patch
  patch -p1 -i "${srcdir}"/git-tcsh-completion-fixes.patch
  patch -p1 -i "${srcdir}"/git-zsh-completion-fixes.patch
  

  local _arch=
  if [ "${CARCH}" == 'x86_64' ]; then
    _arch='amd64'
  elif [ "${CARCH}" == 'i686' ]; then
    _arch='x86'
  fi
  sed -e "s|@ARCH@|${_arch}|g" -i compat/win32/git.manifest

  make configure
}

build() {
  export PYTHON_PATH='/usr/bin/python2'
  cd "${srcdir}/${pkgname}-${pkgver}"

  ./configure --build=${CHOST} \
    --prefix=/usr \
    --sysconfdir=/etc \
    --libexecdir=/usr/lib \
    --with-libpcre2 \
    --with-editor=vim \
    --htmldir=/usr/share/doc/git/html \
    --mandir=/usr/share/man

  make INSTALLDIRS=vendor all
  make -j1 man
  #make -j1 info
  make -C contrib/subtree prefix=/usr all
}

check() {
  export PYTHON_PATH='/usr/bin/python2'
  cd "${srcdir}/${pkgname}-${pkgver}"
  local jobs
  jobs=$(expr "$MAKEFLAGS" : '.*\(-j[0-9]*\).*')
  mkdir -p /tmp/git-test
  # We used to use this, but silly git regressions:
  #GIT_TEST_OPTS="--root=/dev/shm/" \
  # https://comments.gmane.org/gmane.comp.version-control.git/202020
  make \
    NO_SVN_TESTS=y \
    DEFAULT_TEST_TARGET=prove \
    GIT_PROVE_OPTS="$jobs -Q" \
    GIT_TEST_OPTS="--root=/tmp/git-test" \
    test
}

package() {
  export PYTHON_PATH='/usr/bin/python2'
  cd "${srcdir}/${pkgname}-${pkgver}"
  make INSTALLDIRS=vendor DESTDIR="$pkgdir" install
  make INSTALLDIRS=vendor DESTDIR="${pkgdir}" install-man
  #make INSTALLDIRS=vendor DESTDIR="${pkgdir}" install-info

  # bash completion
  mkdir -p "${pkgdir}"/usr/share/bash-completion/completions/
  install -m644 ./contrib/completion/git-completion.bash "${pkgdir}"/usr/share/bash-completion/completions/git
  # fancy git prompt
  mkdir -p "${pkgdir}"/usr/share/git/
  install -m644 ./contrib/completion/git-prompt.sh "${pkgdir}"/usr/share/git/git-prompt.sh
  # subtree installation
  sed "s|libexec/git-core|lib/git-core|" -i contrib/subtree/Makefile
  make -C contrib/subtree prefix=/usr DESTDIR="${pkgdir}" install

  # the rest of the contrib stuff
  cp -a ./contrib/* ${pkgdir}/usr/share/git/

  # scripts are for python 2.x
  sed -i 's|#![ ]*/usr/bin/env python$|#!/usr/bin/env python2|' \
    $(find "$pkgdir" -name '*.py') \
    "${pkgdir}"/usr/lib/git-core/git-p4 \
    "${pkgdir}"/usr/share/git/remote-helpers/git-remote-bzr \
    "${pkgdir}"/usr/share/git/remote-helpers/git-remote-hg
  sed -i 's|#![ ]*/usr/bin/python$|#!/usr/bin/python2|' \
    "${pkgdir}"/usr/share/git/svn-fe/svnrdump_sim.py
}
